package com.app.bimo.library_db.helper;


import com.app.bimo.library_db.SearchData;
import com.app.bimo.library_db.dao.DaoSession;
import com.app.bimo.library_db.dao.SearchDataDao;

import java.util.List;

public class SearchHotHelper {

    private static volatile SearchHotHelper sInstance;
    private static DaoSession daoSession;
    private static SearchDataDao userBeanDao;

    public static SearchHotHelper getsInstance() {
        if (sInstance == null) {
            synchronized (SearchHotHelper.class) {
                if (sInstance == null) {
                    sInstance = new SearchHotHelper();
                    daoSession = DaoDbHelper.getInstance().getSession();
                    userBeanDao = daoSession.getSearchDataDao();
                }
            }
        }
        return sInstance;
    }

    /**
     * 保存用户
     *
     * @param searchData
     */
    public void saveHotSearch(List<SearchData> searchData) {
        userBeanDao.insertOrReplaceInTx(searchData);
    }


    /**
     * 更新用户信息
     *
     * @param searchData
     */
    public void updateUser(SearchData searchData) {

        userBeanDao.update(searchData);
    }

    /**
     * 删除用户
     */
    public void removeUser() {
        userBeanDao.deleteAll();
    }

    /**
     * 随机拿出一个热搜词  给书城首页的输入框用
     *
     * @return
     */
    public SearchData findOneHorSearch(String uid) {
        List<SearchData> list = userBeanDao.queryBuilder().where(SearchDataDao.Properties.Uid.eq(uid)).orderRaw("RANDOM()").list();
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }


    /**
     * 所有热搜数据
     *
     * @return
     */
    public List<SearchData> findHotSearch() {
        return userBeanDao.queryBuilder().list();
    }

}
